package gbench.sandbox.matrix;

import gbench.commonApp.data.DataMatrixApp;
import static gbench.common.tree.LittleTree.DataFrame.*;
import static gbench.common.tree.LittleTree.CronTime.*;
import static gbench.common.tree.LittleTree.IRecord.*;
import static gbench.common.tree.LittleTree.kvp;
import gbench.common.fs.XlsFile.*;
import gbench.common.tree.LittleTree.IRecord;

import java.time.LocalDate;
import java.util.List;

import org.junit.jupiter.api.Test;

import static gbench.common.tree.LittleTree.*;

public class JunitDatataMatrixAndIRecord extends DataMatrixApp{
    
    @Test
    public void foo() {
        final var numbers = "1:3";// 数据长度
        final var dd = cph2(series,RPTA(2,numbers)).collect(dmc);
        final var rec = dd.reduceColumns(e->e.getElems(),IRecord::REC);// 通过 MX 变换成 IRecord
        println("rec");
        println(rec.toString2());
        final var dmx = REC2DMX(Double.class,rec);
        println("dmx");
        println(dmx);
    }
    
    @Test
    public void foo1() {
        var df = DFM("name","zhangsan","sex","man","birth",LocalDate.now());
        var ll = RPTS(100,df);
        println(df);
        var mm = ll.collect(smc);
        println(mm);
        println(df.column(0));
        println(df.row(0));
        println(df.column("birth",LocalDate.class));
        println(df.column("birth",(LocalDate d)->dtf("yyyy").format(d)));
        println("------1------");
        System.err.println("返回的 dd  List<List<LocalDate>>  但 很明显 0,1两列不是LocalDate 类型。原因是 java对于泛型采用的类型擦除机制的实现");
        final List<List<LocalDate>>  dd = df.columns(LocalDate.class);
        dd.stream().map(kvp()).forEach(col->{
            System.out.println(col);
        });
        
        println("-----2-------");
        System.err.println("返回的 pp  List<List<LocalDate>> 这个没有问题的");
        final List<List<Object>> pp = df.columns();
        pp.stream().map(kvp()).forEach(col->{
            System.out.println(col);
        });
    }
    
    /**
     * DataMatrix与IRecord之间的转换
     */
    @Test
    public void bar() {
        // Cells 列表
        final var cc = L(
            L(1,2,3,4,5,7,8,9,0),
            L(1,2,3,4)
        );// 数据单元集合
        
        var mm = new DataMatrix<>(cc,L("A"));
        
        // IRecord的类型转换
        println("数据矩阵");
        println(mm);
        
        var mx= mm.mapByRow(IRecord::REC).collect(omc);
        println("record 集合转数据矩阵");
        println(mx);
        
        var rec_x = mx.reduceRows(DRow::getElems,IRecord::REC);
        System.out.println("数据矩阵转Record(reduceRows) 产生一个 record:");
        System.out.println(rec_x.toString2(e->MFT("#{0}",e),frt(2)));
        
        var rec_y = mx.reduceColumns(DColumn::getElems,IRecord::REC);
        println("数据矩阵转Record(reduceColumns):产生一个 record:");
        println(rec_y.toString2(e->MFT("#{0}",e),frt(2)));
        
    }
    
    @Test
    public void foorbar() {
        final var rec = REC(
            "name",l("张三","李四","王五","某人"),
            "sex",a("男","女","男","未知")
        );
        println(rec.toString2());
        final var mx = new DataMatrix<>(rec.toArray2(e->e+""));
        //  把一列 拆分成列
        final DataMatrix<String>  strx = mx.flatMapColumns( col->l (
            col, // 保持原来的列不变 
            col.map(e->e+col.id()) // 每个元素后面都 补充当前 列号索引的 后缀
               .setName(col.getName()+"_") // 新生成的列 名后面加入 一个"_"后缀
        )); // flatMapColumns
        println(strx);
        println("rec2dmx：DataMatrix<Object>");
        println(REC2DMX(rec));
        println("rec2dmx: DataMatrix<String>");
        println(REC2DMX(String.class,rec));
    }
    
    @Test
    public void qux() {
        final var wts = A(10.7d,8d,9d);
        final var bias = 10d;
        final var thetas = REC2DMX(REC("wts",A(bias,wts),"name","zhangsan,lisi".split(",")));
        println(thetas.filter("wts"));
        println("num\n"+thetas.num("wts","name").toString(frt(2)));
        println("dbl\n"+thetas.dbl("wts","name").toString(frt(2)));
        println("int\n"+thetas.i4("wts","name").toString(frt(2)));
        println("short\n"+thetas.i2("wts","name").toString(frt(2)));
        println("String\n"+thetas.str("wts","name").toString(frt(2)));// 这个时候name 为null
        println("String\n"+thetas.str("name").toString(frt(2)));
        println("Object\n"+thetas.obj("wts","name").toString(frt(2)));// 这个时候name 为null,因为两个列的类型不一样。
    }
}
